<?php
class AnswersController extends AppController {
	var $name = 'Answers';
	
	/**
	 * Get all answers associated with the given question
	 * 
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @return 
	 */
	function index($question_id = null, $survey_id = null) {
		// Make sure the currently authorized user owns the given survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		$answersData =	$this->Answer->find('all', array(
				'fields' => array('Answer.*'),
				'conditions'=>array('Answer.question_id'=>$question_id, // id of question
				'Answer.isDeleted'=>'0'),
				'order'=>array('Answer.viewOrder ASC') // make sure the answer isn't deleted
		));
		
		$this->set('answers', $answersData);
		$this->set('question_id', $question_id);
		$this->set('survey_id', $survey_id);
	}
	
	/**
	 * Delete the given answer
	 * 
	 * @param object $answer_id [optional]
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @return 
	 */
	function delete($answer_id = null, $survey_id = null) {
		// Make sure the currently authorized user owns the given survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		//Set isDeleted = 1 for this question
		$this->Answer->id = $answer_id;
		$this->data['Answer']['isDeleted'] = 1;
		
		$this->Answer->save($this->data);
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Move the given answer up one in the viewOrder
	 * 
	 * @param object $answer_id [optional]
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @param object $view_order [optional]
	 * @return 
	 */
	function moveUp($answer_id = null, $question_id = null, $survey_id = null, $view_order = null) {
		// Make sure the currently authorized user owns the given survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		//Get the viewOrder to swap with
		$data = $this->Answer->find('first', array(
			'fields' => array('Answer.id','Answer.viewOrder'),
			'order' => array('Answer.viewOrder DESC'),
			'conditions' => array('Answer.viewOrder <' => $view_order,
								  'Answer.question_id' => $question_id,
								  'Answer.isDeleted' => '0')
			)
		);
		
		if($data)
		{
			$newViewOrder = $data['Answer']['viewOrder'];
			$oldId = $data['Answer']['id'];
				
			//Set this question to have the new vieworder
			$this->Answer->id = $answer_id;
			$this->data['Answer']['viewOrder'] = $newViewOrder;
			$this->Answer->save($this->data);
			
			//Set the other question to have the current viewOrder
			$this->Answer->id = $oldId;
			$this->data['Answer']['viewOrder'] = $view_order;
			$this->Answer->save($this->data);
		}
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Move the given answer down one in the given viewOrder
	 * 
	 * @param object $answer_id [optional]
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @param object $view_order [optional]
	 * @return 
	 */
	function moveDown($answer_id = null, $question_id = null, $survey_id = null, $view_order = null) {
		// Make sure the currently authorized user owns the given survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		//Get the viewOrder to swap with
		$data = $this->Answer->find('first', array(
			'fields' => array('Answer.id','Answer.viewOrder'),
			'order' => array('Answer.viewOrder ASC'),
			'conditions' => array('Answer.viewOrder >' => $view_order,
								  'Answer.question_id' => $question_id,
								  'Answer.isDeleted' => '0')
			)
		);
		
		if($data)
		{
			$newViewOrder = $data['Answer']['viewOrder'];
			$oldId = $data['Answer']['id'];
				
			//Set this question to have the new vieworder
			$this->Answer->id = $answer_id;
			$this->data['Answer']['viewOrder'] = $newViewOrder;
			$this->Answer->save($this->data);
			
			//Set the other question to have the current viewOrder
			$this->Answer->id = $oldId;
			$this->data['Answer']['viewOrder'] = $view_order;
			$this->Answer->save($this->data);
		}
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Checks to see if the specified survey belongs to the logged in user
	 * 
	 * @param object $survey_id
	 * @return 
	 */
	function _doesSurveyBelongToAuthedUser($survey_id){
		$this->loadModel('Survey');
		
		$active_user = $this->Auth->user();
		$surveys = $this->Survey->find('all', array(
			'conditions' => array('Survey.id' => $survey_id, 'Survey.created_by' => $active_user['User']['id']),
		));
		
		if(empty($surveys)){
			$this->Session->setFlash('Could not perform the specified operation. Please try again');
			return false;
		}
		else return true;
	}
}
?>